import React from 'react'
import Taro from '@tarojs/taro'
import { SpNavBar } from '@/components'
import { View } from '@tarojs/components'
import api from '@/api'
import { Component } from 'react'
import './index.scss'
import { connect } from 'react-redux'
import { updateQRCode, setupQRCode } from '@/store/qrcode'
import { AtMessage } from 'taro-ui'

@connect(({ qrcode }) => ({
    qrcode: qrcode.qrcode,
    codestr: qrcode.codestr
}),
    (dispatch) => ({ dispatch })
)
class ScanQRCode extends Component {
    constructor(props) {
        super(props)
        this.state = {
            scan: null,
        }
    }
    onmarked(type, result) {
        console.log('========', result)
        // var text = '未知: ';
        // switch (type) {
        //     case plus.barcode.QR:
        //         text = 'QR: ';
        //         break;
        //     case plus.barcode.EAN13:
        //         text = 'EAN13: ';
        //         break;
        //     case plus.barcode.EAN8:
        //         text = 'EAN8: ';
        //         break;
        // }
        // alert(text + result);
        this.state.scan.cancel();
        this.state.scan.close();
        Taro.showToast({
            title: '扫码成功,正在核销',
        })
        // Taro.reLaunch({ url: `/pages/index?qr_result=${result}` })
        this.props.dispatch(updateQRCode({ qrcode: true, codestr: result }))
        Taro.navigateBack()

    }
    async startRecognize() {
        if (!this.state.scan) {
            let s = new plus.barcode.Barcode('bcid', [plus.barcode.QR]);
            this.setState({
                scan: s
            }, () => {
                this.state.scan.onmarked = this.onmarked.bind(this);
                this.state.scan.start()
            })

        }


    }
    startScan() {
        this.state.scan.start();
    }
    async componentDidShow() {
    }
    componentDidMount() {

        if (Taro.getEnv() == 'SAPP') {
            // alert(plus.device.vendor)  
            this.requestLocation()
            this.startRecognize()
        }

    }
    // 申请权限
    requestLocation() {
        let that = this
        plus.android.requestPermissions(['android.permission.CAMERA'], function (e) {
            if (e.deniedAlways.length > 0) {	//权限被永久拒绝
                // 弹出提示框解释为何需要权限，引导用户打开设置页面开启
                Taro.showModal({
                    title: '无法使用到您的相机',
                    content: '扫一扫功能需要访问到您的相机，相机权限被拒绝，您可以去设置页面设置您的相机权限',
                    success: function (res) {
                        if (res.confirm) {
                            // that.startRecognize()
                        } else if (res.cancel) {
                            // that.startRecognize()
                        }
                    }
                })
                console.log('Always Denied!!! ' + e.deniedAlways.toString());

            }
            if (e.deniedPresent.length > 0) {	//权限被临时拒绝
                // 弹出提示框解释为何需要定位权限，可再次调用plus.android.requestPermissions申请权限
                console.log('Present Denied!!! ' + e.deniedPresent.toString());
                // that.startRecognize()
            }
            if (e.granted.length > 0) {	//权限被允许
                //调
                console.log('Granted!!! ' + e.granted.toString());
                // that.startRecognize()
            }
        }, function (e) {
            console.log('Request Permissions error:' + JSON.stringify(e));
        });
        if (plus.device.vendor == 'HUAWEI') {
            Taro.atMessage({
                'message': '扫码核销功能需要访问到您的相机进行扫一扫',
            })
        }
    }

    async componentWillUnmount() {
        console.log('2222')
        if (this.state.scan) {
            await this.state.scan.close();
            this.setState({
                scan: null
            })
            this.setState = () => false
        }
    }

    render() {
        return <View>
            <SpNavBar title='扫码'></SpNavBar>
            <View className='content'>
                <View id="bcid"></View>
            </View>
            <AtMessage customStyle={{top:'10px'}}/>

        </View >
    }

}

export default ScanQRCode
